﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;
using System.Text.RegularExpressions;

namespace TextMIS
{
    public partial class frmDoctorFind : Form
    {
        public frmDoctorFind()
        {
            InitializeComponent();
        }
        public static string userName;
        public static string strSql;

        SqlConnection conn;
        SqlDataAdapter da;

        private void frmDoctorFind_Load(object sender, EventArgs e)
        {
            userName = frmLogin.userName;
            conn = new
SqlConnection(frmLogin.SQLstr);

            listView1.GridLines = true;
            listView1.FullRowSelect = true;
            listView1.View = View.Details;
            listView1.Scrollable = true;
            listView1.MultiSelect = false;
            listView1.HeaderStyle = ColumnHeaderStyle.Nonclickable;

            da = new SqlDataAdapter("select BranchNo,BranchName from BranchInfo ", conn);
            DataSet ds = new DataSet();
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            da.Fill(ds, "BranchInfo");
            conn.Close();
            this.tscBranch.ComboBox.DataSource = ds.Tables[0];
            this.tscBranch.ComboBox.DisplayMember = "BranchName";
            this.tscBranch.ComboBox.ValueMember = "BranchNo";

            txtBranch.Enabled = false;
            txtJob.Enabled = false;
            txtName.Enabled = false;
            txtSex.Enabled = false;
            txtJobNo.Enabled = false;

        }

        private void tscBranch_SelectedIndexChanged(object sender, EventArgs e)
        {
            da = new SqlDataAdapter("select DNo,Djob from viewDoctor where BranchName='" + tscBranch.Text + "'", conn);
            DataSet ds = new DataSet();
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            da.Fill(ds, "viewDoctor");

            conn.Close();

            this.tscJob.ComboBox.DataSource = ds.Tables[0];
            this.tscJob.ComboBox.DisplayMember = "Djob";
            this.tscJob.ComboBox.ValueMember = "DNo";
        }

        private void SetupHeaderText()
        {
            listView1.Columns.Add("职工号", 100, HorizontalAlignment.Left);
            listView1.Columns.Add("姓名", 100, HorizontalAlignment.Left);
            listView1.Columns.Add("性别", 100, HorizontalAlignment.Left);
            listView1.Columns.Add("职位", 200, HorizontalAlignment.Left);
            listView1.Columns.Add("所属科室", 120, HorizontalAlignment.Left);

        }

        private void ShowDoctor()
        {
            try
            {
                listView1.Clear();
                da = new SqlDataAdapter("select DNo, Dname,Dsex ,Djob, BranchName from viewDoctor where Djob = '" + tscJob.Text.Trim() + "'and BranchName = '" + tscBranch.Text.Trim() + "'", conn);

                DataTable dt = new DataTable();
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                int count = da.Fill(dt);
                conn.Close();

                SetupHeaderText();
                foreach (DataRow dr in dt.Rows)
                {
                    ListViewItem lvi = new ListViewItem();
                    lvi.SubItems.Clear();
                    lvi.SubItems[0].Text = dr[0].ToString();
                    lvi.SubItems.Add(dr[1].ToString());
                    lvi.SubItems.Add(dr[2].ToString());
                    lvi.SubItems.Add(dr[3].ToString());
                    lvi.SubItems.Add(dr[4].ToString());
                    listView1.Items.Add(lvi);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnPutOut_Click(object sender, EventArgs e)
        {
            txtBranch.Text = listView1.SelectedItems[0].SubItems[4].Text;
            txtJob.Text = listView1.SelectedItems[0].SubItems[3].Text;
            txtName.Text = listView1.SelectedItems[0].SubItems[1].Text;
            txtSex.Text = listView1.SelectedItems[0].SubItems[2].Text;
            txtJobNo.Text = listView1.SelectedItems[0].SubItems[0].Text;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tsbOK_Click(object sender, EventArgs e)
        {
            this.ShowDoctor();
        }

        private void tsbExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
